package com.jiangxiaofei.citycloud.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.bean.WorkLabelBean;
import com.jiangxiaofei.citycloud.event.LabelMessage;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by Administrator on 2017/9/6 0006.
 */

public class RetruitLabelAdapter extends RecyclerView.Adapter<RetruitLabelAdapter.WorkLabelViewHolder> {
    private Context mContext;
    private List<WorkLabelBean> lableList;
    private boolean isChecked;
    private RadioGroup rg_parent;

    public RetruitLabelAdapter(Context mContext, List<WorkLabelBean> lableList, RadioGroup rg_parent) {
        this.mContext = mContext;
        this.lableList = lableList;
        this.rg_parent = rg_parent;
    }

    public RetruitLabelAdapter(Context mContext, List<WorkLabelBean> lableList) {
        this.mContext = mContext;
        this.lableList = lableList;
    }

    @Override
    public WorkLabelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_work_label_layout, parent, false);
        WorkLabelViewHolder holder = new WorkLabelViewHolder(v);
        return holder;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(final WorkLabelViewHolder holder, final int position) {
        holder.rb_label.setText(lableList.get(position).getName());
        holder.rb_label.setTextColor(lableList.get(position).isChecked() ?
                mContext.getResources().getColor(R.color.maintab_text_selected_color) :
                Color.BLACK);
        holder.rb_label.setBackground(lableList.get(position).isChecked() ?
                mContext.getResources().getDrawable(R.drawable.shape_item_label_select) :
                mContext.getResources().getDrawable(R.drawable.shape_item_label));
        holder.rb_label.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!lableList.get(position).isChecked()) {
                    holder.rb_label.setTextColor(mContext.getResources().getColor(R.color.maintab_text_selected_color));
                    holder.rb_label.setBackground(mContext.getResources().getDrawable(R.drawable.shape_item_label_select));
                } else {
                    holder.rb_label.setTextColor(Color.BLACK);
                    holder.rb_label.setBackground(mContext.getResources().getDrawable(R.drawable.shape_item_label));
                }
                lableList.get(position).setChecked(!lableList.get(position).isChecked());
                LabelMessage labelMessage = new LabelMessage(lableList);
                EventBus.getDefault().post(labelMessage);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lableList.size();
    }

    class WorkLabelViewHolder extends RecyclerView.ViewHolder {
        TextView rb_label;

        public WorkLabelViewHolder(View itemView) {
            super(itemView);
            rb_label = itemView.findViewById(R.id.item_rb_work_label);
        }
    }
}
